package cn.qzzg.service.impl;

import cn.qzzg.common.exceptlion.CommonException;
import cn.qzzg.enums.CodeEnum;
import cn.qzzg.pojo.PotTime;
import cn.qzzg.service.PotTimeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.qzzg.pojo.TestTube;
import cn.qzzg.service.TestTubeService;
import cn.qzzg.mapper.TestTubeMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import  cn.qzzg.dto.TestTubeDto;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 九九
 * @description 针对表【test_tube(试管信息表)】的数据库操作Service实现
 * @createDate 2023-03-25 21:51:39
 */
@Service
public class TestTubeServiceImpl extends ServiceImpl<TestTubeMapper, TestTube>
        implements TestTubeService {
    /**
     * 通用条件构造器
     */
    private LambdaQueryWrapper<TestTube> wrapper;

    @Resource
    PotTimeService potTimeService;

    @Resource
    TestTubeMapper testTubeMapper;

    /**
     * 查询数据是否存在
     *
     * @param objId 需要查询的对象ID
     * @return 是否存在
     */
    @Override
    public TestTube check(String objId) {
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(objId), TestTube::getTestTubeId, objId);
        return getOne(wrapper);
    }

    @Override
    public TestTube findTestTubeInfo(TestTube testTube) {
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(testTube.getTestTubeId()), TestTube::getTestTubeId, testTube.getTestTubeId())
                .eq(testTube.getPotTimeId()!=null,TestTube::getPotTimeId, testTube.getPotTimeId())
                .eq( testTube.getPotTimeAddress()!=null,TestTube::getPotTimeAddress, testTube.getPotTimeAddress())
                .eq(testTube.getNewPotTimeId()!=null,TestTube::getNewPotTimeId, testTube.getNewPotTimeId())
                .eq(testTube.getNewPotTimeAddress()!=null,TestTube::getNewPotTimeAddress, testTube.getNewPotTimeAddress());
        return getOne(wrapper);
    }

    /**
     * 通过ID查询全部数据
     *
     * @param objId 需要查询的对象ID
     * @return 结果对象
     */
    @Override
    public List<TestTube> findAllByTransferBoxId(String objId) {
        if (ObjectUtils.isEmpty(objId))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestTube::getTransferBoxId, objId)
                .orderByDesc(TestTube::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<TestTube> findAllByPotTimeId(String objId) {
        if (ObjectUtils.isEmpty(objId)) {
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        }
        List<TestTube> list;

        PotTime potTimeInfo = potTimeService.findPotTimeInfo(Integer.parseInt(objId));
        if(potTimeInfo!=null&& potTimeInfo.getPotStatus()!=null&&potTimeInfo.getPotStatus()==1){
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TestTube::getNewPotTimeId, objId)
                    .orderByDesc(TestTube::getCreateTime);
            list = list(wrapper);
        }else {
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TestTube::getPotTimeId, objId)
                    .orderByDesc(TestTube::getCreateTime);
            list = list(wrapper);
        }
        return list;
    }

    /**
     * 通过ID查询一条数据
     *
     * @param objId 需要查询的对象ID
     * @return 结果对象
     */
    @Override
    public TestTube findById(String objId) {
        if (ObjectUtils.isEmpty(objId))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        return getById(objId);
    }


    /**
     * 添加一条数据
     *
     * @param obj 需要添加的对象
     * @return 添加的对象ID
     */
    @Override
    public String saveOne(TestTube obj) {
        if (ObjectUtils.isEmpty(obj))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(obj.getTestTubeId()), TestTube::getTestTubeId, obj.getTestTubeId());
        if (count(wrapper) > 0)
            throw new CommonException(CodeEnum.CUSTOM_ERROR_F0101);
        save(obj);
        return obj.getTestTubeId();
    }

    /**
     * 通过ID删除一条数据
     *
     * @param objId 需要删除的对象ID
     * @return 删除是否成功
     */
    @Override
    public boolean removeOne(String objId) {
        if (ObjectUtils.isEmpty(objId))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        removeById(objId);
        return true;
    }

    /**
     * 根据ID批量删除数据
     *
     * @param idList 需要删除的对象ID集合
     */
    @Override
    public void batchRemove(List<String> idList) {
        if (ObjectUtils.isEmpty(idList) || idList.size() == 0)
            throw new CommonException(CodeEnum.USER_ERROR_A0410);
        removeBatchByIds(idList);
    }

    /**
     * 修改数据
     *
     * @param obj 需要修改的对象
     * @return 修改是否成功
     */
    @Override
    public boolean updateOne(TestTube obj) {
        if (ObjectUtils.isEmpty(obj))
            throw new CommonException(CodeEnum.USER_ERROR_A0410);

        updateById(obj);
        return true;
    }

    @Override
    public boolean updateInfo(TestTube obj) {
        return updateById(obj);
    }


    /**
     * 分页查询
     *
     * @return 分页查询结果集合
     */
    @Override
    public Page<TestTube> findPage(TestTubeDto  testTubeDto) {
        //锅次ID
        int potTimeId = testTubeDto.getData().getPotTimeId();
        Page pages;
        //判断是否为复查锅
        PotTime potTimeInfo = potTimeService.findPotTimeInfo(potTimeId);
        wrapper = new LambdaQueryWrapper<>();
        if(potTimeInfo!=null&& potTimeInfo.getPotStatus()!=null&&potTimeInfo.getPotStatus()==1){
            wrapper.eq(TestTube::getNewPotTimeId, String.valueOf(testTubeDto.getData().getPotTimeId()))
                    .ne(TestTube::getPotTimeAddress, "null")
                    .orderByDesc(TestTube::getCreateTime);
        }else {
            wrapper.eq(TestTube::getPotTimeId, String.valueOf(testTubeDto.getData().getPotTimeId()))
                    .ne(TestTube::getPotTimeAddress, "null")
                    .orderByDesc(TestTube::getCreateTime);
        }
        Page page=testTubeDto.getPage();

//        page.setSize(-1L);
        pages = page(page, wrapper);
        return pages;
    }
}




